/**
 * @(#) InrecMediator.as
 *
 * @date		2012. 11. 07.
 * @version		1.0
 * @author		Park, Jutae
 * @description	사내추천신청 - Mediator
 * 				Inrec를 조회하고 신청하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.sepcost.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.sepcost.SepCostFacade;
	import com.skt.common_hr.sepcost.events.ComboBoxYearEvent;
	import com.skt.common_hr.sepcost.model.SepCostProxy;
	import com.skt.common_hr.sepcost.model.vo.SepCostVO;
	import com.skt.common_hr.sepcost.view.components.ReqSepCostInrec;
	import com.skt.common_hr.sepcost.view.components.SepCostView;
	
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class SepCostMediator extends OverViewMediator
	{
		public static const NAME:String = "InrecMediator"; 


		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function SepCostMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		
		override protected function initializeView():void
 		{
			super.initializeView();
 		}
		
 		override protected function activatedView():void
 		{
 			setEventListeners();
 			view.setDateRange();
 			loadList();
 		} 
 		
 		override protected function closedView():void
 		{
			removeEventListeners(); 			
 		}
 		
 		private function setEventListeners():void
 		{
 			view.addEventListener(SepCostView.Serach, loadListEvt);
 			view.addEventListener(ReqSepCostInrec.Insert, onSinChung);
 			view.addEventListener(SepCostView.sinChungCheck, onSinChungCheck);
 			view.addEventListener(SepCostView.Delete, onDelete);
 			view.addEventListener(ComboBoxYearEvent.SELECT_YEAR_DATA, onComboYear);
 		}
 		
 		private function removeEventListeners():void
 		{
 			view.removeEventListener(SepCostView.Serach, loadListEvt);
 			view.removeEventListener(SepCostView.sinChungCheck, onSinChungCheck);
 			view.removeEventListener(SepCostView.Insert, onSinChung);
 			view.removeEventListener(SepCostView.Delete, onDelete);
 		}
 		
 		/* 분리과금 추가지원클릭시 List 조회 */
 		private function loadListEvt(e:Event):void 
 		{
 			view.vs.selectedIndex = 0;
 			
 			var proxy:SepCostProxy = facade.retrieveProxy(SepCostProxy.NAME) as SepCostProxy;
 			proxy.load(Variables.myPernr, view.sDay, view.eDay);
 		}
 		
 		/* 분리과금 추가지원클릭시 List 조회 */
 		private function loadList():void 
 		{
 			view.vs.selectedIndex = 0;
 			
 			var proxy:SepCostProxy = facade.retrieveProxy(SepCostProxy.NAME) as SepCostProxy;
 			proxy.load(Variables.myPernr, view.sDay, view.eDay);
 		}
 		
 		/* 분리과금 추가지원신청 클릭시 */
 		private function onSinChung(e:Event):void
 		{
 			var obj:SepCostVO = view.reqView.dataVoList;
			
 			var proxy:SepCostProxy = facade.retrieveProxy(SepCostProxy.NAME) as SepCostProxy;
 			proxy.insert(obj);
 		}
 		 
		/* 통신비신청 신청 가능여부 */
 		private function onSinChungCheck(e:Event):void
 		{
 			var proxy:SepCostProxy = facade.retrieveProxy(SepCostProxy.NAME) as SepCostProxy;
 			proxy.loadCheck(Variables.myPernr);
 			 			
 		}
 		
		/* 분리과금 추가지원삭제 클릭시 */
		private function onDelete(event:Event):void
		{
			var obj:SepCostVO = view.dg.selectedItem as SepCostVO;
			
 			var proxy:SepCostProxy = facade.retrieveProxy(SepCostProxy.NAME) as SepCostProxy;
 			proxy.del(obj); 
			
		}
		
 		/* 메인화면 대상년도 변경시 */
 		private function onComboYear(event:ComboBoxYearEvent):void
 		{
 			var cbProxy:SepCostProxy = facade.retrieveProxy(SepCostProxy.NAME) as SepCostProxy;
//			cbProxy.load(Variables.myPernr, event.comData);
 		}
 		
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat([
					SepCostFacade.LOAD_LIST_COMPLETE,
					SepCostFacade.LOAD_LIST_FAILED,
					SepCostFacade.INSERT_COMPLETE,
					SepCostFacade.INSERT_FAILED,
					SepCostFacade.DELETE_COMPLETE,
					SepCostFacade.DELETE_FAILED,
					SepCostFacade.LOAD_CHECK_COMPLETE,
					SepCostFacade.LOAD_CHECK_FAILED,
					SepCostFacade.OK_REQUEST_INPUT,
					SepCostFacade.CANCEL_REQUEST_INPUT
					]);
		}
		
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName()) 
			{
				case SepCostFacade.LOAD_CHECK_COMPLETE:
					onReadCheck();
					break;
				case SepCostFacade.LOAD_CHECK_FAILED:
					Alert.show((SepCostProxy(note.getBody()).failedCheckAlert));
					break;
				case SepCostFacade.LOAD_LIST_COMPLETE:
					onLoadList(note.getBody() as SepCostProxy);
					break;
				case SepCostFacade.LOAD_LIST_FAILED:
					break;
				case SepCostFacade.OK_REQUEST_INPUT:
					view.vs.selectedIndex = 0;
					loadList();
					break;
				case SepCostFacade.CANCEL_REQUEST_INPUT:
					Alert.show((SepCostProxy(note.getBody()).failedAlert));
					break;
				case SepCostFacade.INSERT_COMPLETE:
					loadList();
					loadListPop(note.getBody() as SepCostProxy);
					break;		
				case SepCostFacade.DELETE_COMPLETE:
					loadList();
					break;
				default:
					break;
			}
		}
		
 		private function onLoadList(proxy:SepCostProxy):void
 		{
 			view.inrecColl = proxy.inrecList;
 			view.e_joint = proxy.e_joint;
 		}
 		
 		/* 분리과금 추가지원 신청 조회 체크 성공시 */
		private function onReadCheck():void
		{
			var vo:SepCostVO = new SepCostVO();
			view.reqView.setLoad(vo, "NEW");
			view.vs.selectedIndex = 1;
//			var proxy:SepCostProxy = facade.retrieveProxy(SepCostProxy.NAME) as SepCostProxy;
// 			proxy.readLoad(_vo);
		}
		
		/* 분리과금 추가지원 신청서 조회 */
 		private function loadListPop(proxy:SepCostProxy):void 
 		{
 			view.readListPopArr = proxy.readList;
 			view.clickPopupBtn();
 		}
 		
 		
		private function onNotePrint(url:String):void
		{ 
			var req:URLRequest = new URLRequest("../ace_inrec/"+url);
			navigateToURL(req);
		}
 		
        private function get view():SepCostView
        {
            return viewComponent as SepCostView;
        }
		
		
	}
}